package com.danger.Activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.danger.Adapter.MyRecyclerViewAdapter;
import com.danger.ItemDecoration.DividerGridItemDecoration;
import com.danger.ItemDecoration.DividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private List<String> list;
    private Toolbar toolbar;
    private MyRecyclerViewAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initEvent();
    }

    private void initView() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        //一定要加上这一句，为RecyclerView设置布局管理器，不然item显示不出来
        //recyclerView线性布局
//        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        //recyclerView表格布局
//        recyclerView.setLayoutManager(new GridLayoutManager(this,4));
        //recyclerView表格布局瀑布流式布局
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(4,
                StaggeredGridLayoutManager.HORIZONTAL));
        list = new ArrayList<>();
        list.add("Schedule");
        list.add("Schedule");
        list.add("Schedule");
        list.add("Schedule");
        list.add("Schedule");
        list.add("Schedule");
        list.add("Schedule");
        list.add("Schedule");
        list.add("Schedule");
        list.add("Schedule");
        list.add("Schedule");
        list.add("Schedule");
        list.add("Schedule");
        list.add("Schedule");
        list.add("Schedule");
        list.add("Schedule");
        list.add("Schedule");
        list.add("Schedule");
        list.add("Schedule");
        list.add("Schedule");
        list.add("Schedule");
        list.add("Schedule");
        list.add("Schedule");
        list.add("Schedule");
        list.add("Schedule");
        list.add("Schedule");
        list.add("Schedule");
        list.add("Schedule");
        list.add("Schedule");
        list.add("Schedule");
        list.add("Schedule");
        list.add("Schedule");
        list.add("Schedule");
//        recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL_LIST));
        recyclerView.addItemDecoration(new DividerGridItemDecoration(this));
        adapter = new MyRecyclerViewAdapter(list, this);
        recyclerView.setAdapter(adapter);
    }

    private void initEvent() {
        adapter.setOnItemClickLitener(new MyRecyclerViewAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(MainActivity.this, position + " click",
                        Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onItemLongClick(View view, int position) {
                Toast.makeText(MainActivity.this, position + " long click",
                        Toast.LENGTH_SHORT).show();
                adapter.removeData(position);
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.add:
                adapter.addData(1);
                break;
            case R.id.delete:
                adapter.removeData(1);
                break;
            default:
                break;
        }
        return true;
    }
}